package com.tledu.spring.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tledu.spring.core.exception.CustomException;
import com.tledu.spring.core.vo.BasicVo;
import com.tledu.spring.dao.CusMapper;
import com.tledu.spring.domain.Cus;
import com.tledu.spring.service.ICusService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Lsj
 * @version 1.0
 * @date 2021/7/5 9:14
 */
@Service
@AllArgsConstructor
public class CusServiceImpl implements ICusService {
    private final CusMapper cusMapper;
    /**
     * 查询
     *
     * @param page  第几页
     * @param limit 几条数据
     * @return
     */
    @Override
    public BasicVo<Cus> selectByAll(Integer page, Integer limit) {
        BasicVo basicVo = new BasicVo();
        basicVo.setCode(0);
        basicVo.setMsg("");
        IPage<Cus> cusIPage = new Page<>(page,limit);
        IPage<Cus> result = cusMapper.selectPage(cusIPage,null);
        basicVo.setCount(result.getTotal());
        // 获得记录
        List<Cus> cusList = result.getRecords();
        List<Cus> cusVoList = new ArrayList<>();
        for (Cus cus:cusList){
            cusVoList.add(cus);
        }
        basicVo.setData(cusVoList);
        return basicVo;
    }

    /**
     * 搜索
     *
     * @param page  第几页
     * @param limit 几条数据
     * @param name  名字
     * @return
     */
    @Override
    public BasicVo<Cus> selectByAll(Integer page, Integer limit, String name) {
        BasicVo basicVo = new BasicVo();
        basicVo.setCode(0);
        basicVo.setMsg("");
        IPage<Cus> cusIPage = new Page<>(page,limit);
        IPage<Cus> result = cusMapper.selectPage(cusIPage,null);
        basicVo.setCount(result.getTotal());
        // 获得记录
        List<Cus> cusList = result.getRecords();
        List<Cus> cusVoList = new ArrayList<>();
        for (Cus cus:cusList){
            if (cus.getC_name().contains(name)||name.equals("")){
                cusVoList.add(cus);
            }
        }
        basicVo.setData(cusVoList);
        return basicVo;
    }

    /**
     * insert record to table
     *
     * @param record the record
     * @return insert count
     */
    @Override
    public int insert(Cus record) {
        return cusMapper.insert(record);
    }

    /**
     * 登录
     *
     * @param c_name
     * @param c_password
     * @return
     */
    @Override
    public Cus login(String c_name, String c_password) {
        Cus cus = cusMapper.selectAllByC_name(c_name);
        if (cus==null){
            throw new CustomException("用户不存在");
        }
        if (!SecureUtil.md5(c_password).equals(cus.getC_password())){
            throw new CustomException("密码不正确");
        }
        return cus;
    }

    /**
     * 根据id查询用户
     *
     * @param c_id
     * @return
     */
    @Override
    public Cus selectAllByC_id(Integer c_id) {
        return cusMapper.selectAllByC_id(c_id);
    }

    /**
     * 修改
     *
     * @param updated
     * @param c_id
     * @return
     */
    @Override
    public int updateByC_id(Cus updated, Integer c_id) {
        return cusMapper.updateByC_id(updated,c_id);
    }
    /**
     * 删除
     *
     * @param cid
     * @return
     */
    @Override
    public int deleteByC_id(String cid) {
        List<String> item = Arrays.asList(StringUtils.split(cid,","));
        List<Integer> intItem = new ArrayList<>();
        for (String term:
                item) {
            intItem.add(Integer.parseInt(term));
        }
        for (Integer c_id:intItem){
            if (c_id!=null){
                cusMapper.deleteByC_id(c_id);
            }
        }
        return 1;
    }

}
